﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;


namespace SPSolutionFactory.VWPTestLista
{
    public partial class VWPTestListaUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            APISharePoint myAPI = new APISharePoint();

            SPWeb web  = SPContext.Current.Site.OpenWeb();
            if (!APISharePoint.IsExistList(web, "Lista1"))
            {
                SPList myLista = myAPI.GetList(SPContext.Current.Site.Url, "Lista1");
                Response.Write("info da lista: " + myLista.Title);
            }
            else
            {
                Response.Write("Lista ja Existe");
            }
        }

        protected void btnAddField_Click(object sender, EventArgs e)
        {
            string mensagem = string.Empty;

            //Add Field choice to list "Libro".
            try
            {
                SPWeb web = SPContext.Current.Site.OpenWeb();
                SPList listLibro = web.Lists["Libro"];
                SPList listAuthor = web.Lists["Author"];

                Guid listId = listAuthor.ID;
                listLibro.Fields.AddLookup("Author", listId, false);
                listLibro.Update();
                mensagem = "Field created successufuly";
            }
            catch
            {
                mensagem = "Tem listas não criadas";
            }
            finally
            {
                lblMensagem2.Text = mensagem;
            }
        }
        protected void btnCrearLista_Click(object sender, EventArgs e)
        {
            string mensagem = string.Empty;
            try
            {
                SPWeb web = SPContext.Current.Site.OpenWeb();
                String nameList = "Author";
                SPList newList = null;
                
                if (!APISharePoint.IsExistList(web, nameList))
                {
                    Guid listId = web.Lists.Add(nameList, "All authors", SPListTemplateType.GenericList);
                    newList = web.Lists[listId];
                    newList.OnQuickLaunch = true;
                    newList.Fields.Add("Category", SPFieldType.Text, false);
                    newList.Fields.Add("Telephone", SPFieldType.Text, false);
                    newList.Update();
                    mensagem = string.Format("List {0} with ID {1} Adding successfuly", newList.Title, listId);
                }
            }
            catch
            {
                mensagem = string.Format("List {0} canont created");
            }
            finally
            {
                lblMensagem.Text = mensagem;
            }
        }
    }
}
